package method

import (
	"fmt"

	"golang.org/x/crypto/bcrypt"
)

// HashPassword 加密密码
func HashPwd(password string) (string, error) {
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", fmt.Errorf("password hashing failed: %v", err)
	}
	return string(hashedPassword), nil
}

// ComparePasswords 比对密码
func ComparePwd(hashedPassword string, plainPassword string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(plainPassword))
	if err != nil {
		return false
	}
	return true
}
